(function(pageName){
  sugarcane.module[pageName] = function(){
    $('.j-pagescreen').addClass('fn-hide');
    var urlParams = sugarcane.getUrlParams(),
      docType = 1,newsId = urlParams['newsId'],
      id = urlParams['id'];
    if ($('#j-screen-'+pageName+id).length) {
      var pageScreen = $('#j-screen-'+pageName+id);
      pageScreen.removeClass('fn-hide');
      var _scrollTop = pageScreen.data('scrolltop') || 0;
      setTimeout(function(){
        $(window).scrollTop(_scrollTop);
      }, 10);
      return false;
    }
    var pageScreen = $('<div class="j-pagescreen" id="j-screen-'+pageName+id+'"></div>');
    $('body').append(pageScreen);

    detailShow();

    // 获取新闻
    var detailData = {};
    function detailShow(){
      if (id) {
        sugarcane.ajaxApi({
          "url": "/interactive/questionDetail",
          "data": {"id": id,"newsId": newsId},
          "callback": function(res){
            if (res['code'] == 0) {
              pageScreen.html('');
              document.title = res.data.title;
              sugarcane.twoShare({
                "id": res.data.id,
                "title": res.data.title,
                "desc": '分享来自经济日报客户端，更多精彩请下载http://d.jingjiribao.cn/index.html',
                "imgUrl": res.data.shareImage,
                "link": res.data.shareUrl,
                "docType": res.data.docType
              });
              if(res.data.docType){
                docType = res.data.docType;
              }
              sugarcane.module.load('question', function(questionFn){
                var questionObj = questionFn(res['data'], function(){
                  detailShow();
                });
                pageScreen.append(questionObj);
                // shareBanner();
                sugarcane['shareBanner']({
                  'id': newsId,
                  'docType': docType,
                  'containObj': pageScreen 
                });
                
              });
            } else {
              showerrorView();
              peach.hiddenAppbar();
              // peach.alert(res['message']);
            }
          }
        });
      }
    }
    
    function showerrorView(){
      var clientHeight = document.documentElement.clientHeight;
      pageScreen.css('position','relative').css('height',clientHeight+'px');
      var errorviewObj = $(`<div class="app-errorview">
          <img class="img" src="/static/img/isempty.png">
        <p class="error-tip">暂时没有数据</p>
      </div>`);
       pageScreen.append(errorviewObj);
    }
  };

  sugarcane.module['state'][pageName] = 'load';
  sugarcane.module.callback(pageName);
}('questionaires'));
